package com.sftz.framework.service.aliyun;

import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.sftz.framework.service.aliyun.config.AliyunAkConfig;
import com.uxuexi.core.common.util.Util;

/**
 * 阿里云视频点播上传服务
 * @author   朱晓川
 * @Date	 2017年11月22日
 */
@IocBean(name = "aliyunVideoUploadService")
public class AliyunVideoUploadService {

	private static final Log log = Logs.get();

	DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", AliyunAkConfig.ACCESS_KEY_ID,
			AliyunAkConfig.ACCESS_KEY_SECRET);

	/**
	 * 阿里云视频点播上传视频文件，返回视频id
	 * @param vd
	 * @param fileName   指定上传文件绝对路径(文件名称必须包含扩展名)
	 */
	public String uploadFile(AliyunVideoDesc vd, String fileName) {
		if (Util.isEmpty(vd)) {
			throw new IllegalArgumentException("视频描述对象不能为空");
		}

		String title = vd.getTitle(); //视频标题
		if (Util.isEmpty(title)) {
			throw new IllegalArgumentException("视频标题不能为空");
		}

		if (Util.isEmpty(fileName)) {
			throw new IllegalArgumentException("视频文件绝对路径不能为空");
		}

		UploadVideoRequest request = new UploadVideoRequest(AliyunAkConfig.ACCESS_KEY_ID,
				AliyunAkConfig.ACCESS_KEY_SECRET, title, fileName);
		request.setCateId(vd.getCateId());
		request.setTags(vd.getTags());
		request.setDescription(vd.getDescription()); //视频描述
		request.setCoverURL(vd.getCoverURL()); //视频自定义封面URL
		//		request.setCallback("http://callback.sample.com"); //设置上传完成后的回调URL
		request.setPartSize(10 * 1024 * 1024L); //可指定分片上传时每个分片的大小，默认为10M字节
		request.setTaskNum(1); //可指定分片上传时的并发线程数，默认为1，(注：该配置会占用服务器CPU资源，需根据服务器情况指定）
		request.setIsShowWaterMark(false); //是否使用水印

		try {
			UploadVideoImpl uploader = new UploadVideoImpl();
			UploadVideoResponse response = uploader.uploadVideo(request);
			return response.getVideoId(); //上传成功后返回视频ID
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e.getCause());
			log.info(e.getMessage());
			return null;
		}
	}

	/**
	 * 阿里云视频点播,获取视频上传地址和凭证
	 * @param vd
	 * @param fileName   指定上传文件绝对路径(文件名称必须包含扩展名)
	 */
	public CreateUploadVideoResponse createUploadVideo(AliyunVideoDesc vd, String fileName) {
		if (Util.isEmpty(vd)) {
			throw new IllegalArgumentException("视频描述对象不能为空");
		}

		String title = vd.getTitle(); //视频标题
		if (Util.isEmpty(title)) {
			throw new IllegalArgumentException("视频标题不能为空");
		}

		if (Util.isEmpty(fileName)) {
			throw new IllegalArgumentException("视频源文件名称不能为空");
		}

		CreateUploadVideoRequest request = new CreateUploadVideoRequest();
		CreateUploadVideoResponse response = null;
		try {
			/*必选，视频源文件名称（必须带后缀, 支持 ".3gp", ".asf", ".avi", ".dat", ".dv", ".flv", ".f4v", ".gif", ".m2t", ".m3u8", ".m4v", ".mj2", ".mjpeg", ".mkv", ".mov", ".mp4", ".mpe", ".mpg", ".mpeg", ".mts", ".ogg", ".qt", ".rm", ".rmvb", ".swf", ".ts", ".vob", ".wmv", ".webm"".aac", ".ac3", ".acm", ".amr", ".ape", ".caf", ".flac", ".m4a", ".mp3", ".ra", ".wav", ".wma"）*/
			request.setFileName(fileName);
			//必选，视频标题
			request.setTitle(title);
			//可选，分类ID
			request.setCateId(vd.getCateId());
			//可选，视频标签，多个用逗号分隔
			request.setTags(vd.getTags());
			//可选，视频描述
			request.setDescription(vd.getDescription());

			DefaultAcsClient client = new DefaultAcsClient(profile);
			response = client.getAcsResponse(request);
		} catch (ServerException e) {
			log.info("CreateUploadVideoRequest Server Exception:");
			e.printStackTrace();
			return null;
		} catch (ClientException e) {
			log.info("CreateUploadVideoRequest Client Exception:");
			e.printStackTrace();
			return null;
		}
		return response;
	}

	/**
	 * 阿里云视频点播,刷新视频上传凭证
	 * @param videoId  视频id
	 */
	public RefreshUploadVideoResponse refreshUploadVideo(String videoId) {
		RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
		RefreshUploadVideoResponse response = null;

		if (Util.isEmpty(videoId)) {
			throw new IllegalArgumentException("视频id不能为空");
		}

		try {
			request.setVideoId(videoId);
			DefaultAcsClient client = new DefaultAcsClient(profile);
			response = client.getAcsResponse(request);
		} catch (ServerException e) {
			log.info("RefreshUploadVideoRequest Server Exception:");
			e.printStackTrace();
		} catch (ClientException e) {
			log.info("RefreshUploadVideoRequest Client Exception:");
			e.printStackTrace();
		}
		return response;
	}
}